<?php 
namespace town\controller;

use town\model\User;
use town\base\Store;

/**
 * 用户授权
 */
class Auth
{
    public function loginView()
    {
        return view('login');
    }


    /**
     * 登陆
     */
    public function login()
    {
        $ret  = User::auth();
        $csrf = Store::val('csrf_token');
        $data = ['csrf_token'=>$csrf];
        if (is_string($ret)) {
            $data['code'] = 1;
            $data['msg']  = $ret;
        } elseif (is_array($ret)) {
            $data['code'] = 0;
            $data['msg']  = '登陆成功!!!';
            $_SESSION['auth_id'] = $ret['id'];
        } else {
            $data['code'] = 2;
            $data['msg']  = '未知错误!!!';
        }
        return response()->json($data);
    }


    /**
     * 退出
     */
    public function out()
    {
        $_SESSION['auth_id'] = null;
        echo 'ok';
    }
}

